package com.example.howard.mytcpclient;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public class ClientList extends Activity {
    private ListView clientlist;
    private Button resbtn;
    private ShowClientView showClientView = new ShowClientView(this);
    private ShowClientViewAdapter showClientViewAdapter;
    private List<String> contentList = new ArrayList<>();
//    private List<String> ClientList = new ArrayList<>();
    public ClientThread clientThreadsent;
    public ClientShowHandler clientShowHandler;
    private MyApplication mApplication;
    private ConnectService connectService;
//    public static ClientList clientListthis;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.client_list);
        clientlist = (ListView) findViewById(R.id.cliList);
        resbtn= (Button) findViewById(R.id.reClient);
        showClientViewAdapter = new ShowClientViewAdapter(this, contentList);
        clientlist.setAdapter(showClientViewAdapter);
        mApplication = (MyApplication)getApplication();
        connectService=mApplication.myConnectService;
//        clientThreadsent=new ClientThread(mApplication.IP,mApplication.Port);
        clientThreadsent = connectService.getClientThread();
        clientShowHandler=new ClientShowHandler();
        mApplication.getClientList(this);
        clientThreadsent.getClientList(this);
        resbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showClientView.clear();
//                new Thread(clientThreadsent).start();
                Message message = new Message();
                message.what = 0x853;
                message.obj = "resclient";
                clientThreadsent.sendHandler.sendMessage(message);
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();




    }
    //传递Handler值
    public ClientShowHandler recHandler(){
        return clientShowHandler;
    }
    public ClientList recClientList (){
        return this;
    }
    //对列表进行转变
    private void modifyList(List<String> result, List<String> branch) {
        result.clear();
        for (int i = 0; i < branch.size(); i++) {
            result.add(branch.get(i));
        }
    }
//    //启动和绑定服务
//    private void initService() {
//
//        startService(new Intent(this, ConnectService.class));
//        bindService(new Intent(this, ConnectService.class), connection, BIND_AUTO_CREATE);
//
//    }
//
//    //绑定服务参数设置
//    ServiceConnection connection = new ServiceConnection() {
//        @Override
//        public void onServiceConnected(ComponentName name, IBinder binder) {
//
//            connectService = ((ConnectService.ConnectBinder) binder).getService();
//
////            connectService.insertReceiverListener(new Handler());
//        }
//
//        @Override
//        public void onServiceDisconnected(ComponentName name) {
//
//        }
//    };
    public class ClientShowHandler extends Handler {

        @Override
        public void handleMessage(Message msg1) {

            switch (msg1.what) {

                case 0x888:

                    showClientView.addContent(msg1.obj.toString());
                    modifyList(contentList, showClientView.getCurrentContentList());
                    showClientViewAdapter.notifyDataSetChanged();
                    break;
            }
            super.handleMessage(msg1);
        }
    }
}
